# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=gtest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.17.0
pkgrel=1
pkgdesc="Google Testing and Mocking Framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/google/googletest"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/google/googletest/releases/download/v${pkgver}/googletest-${pkgver}.tar.gz"
        01-fix-pkgconfig-files.patch)
sha256sums=('65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c'
            '7112f4bebfa4bd6658c5c886242d0dff820d00ff9204edb667ff64618107f84a')

prepare() {
  cd googletest-${pkgver}
  patch -p1 -i "${srcdir}"/01-fix-pkgconfig-files.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DGOOGLETEST_VERSION="${pkgver}" \
      -DBUILD_SHARED_LIBS=OFF \
      ../googletest-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DGOOGLETEST_VERSION="${pkgver}" \
      -DBUILD_SHARED_LIBS=ON \
      ../googletest-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  mkdir -p "${pkgdir}"/${MINGW_PREFIX}/src/gtest/cmake
  install -m 0644 "${srcdir}"/googletest-${pkgver}/googletest/cmake/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/cmake/

  install -Dm 0644 "${srcdir}"/googletest-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/gtest/LICENSE
  install -Dm 0644 "${srcdir}"/googletest-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/gmock/LICENSE
}
